package com.ayf.payment.game.api.wx;

import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.utils.EncryptUtils;
import com.ayf.payment.game.api.utils.SortUtils;
import com.swwx.charm.commons.lang.base.BasicTO;


/**
 * @Description: 微信验证实体类
 * @Author: zh-liang
 */
public class CheckWeChat extends BasicTO {
    private static final long serialVersionUID = 1L;

    /**
     * 微信加密签名
     */
    private String signature;
    /**
     * 时间戳
     */
    private String timestamp;
    /**
     * 随机数
     */
    private String nonce;
    /**
     * 随机字符串
     */
    private String echostr;

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getNonce() {
        return nonce;
    }

    public void setNonce(String nonce) {
        this.nonce = nonce;
    }

    public String getEchostr() {
        return echostr;
    }

    public void setEchostr(String echostr) {
        this.echostr = echostr;
    }

    @Override
    public String toString() {
        return "CheckWeChat{" +
                "signature='" + signature + '\'' +
                ", timestamp='" + timestamp + '\'' +
                ", nonce='" + nonce + '\'' +
                ", echostr='" + echostr + '\'' +
                '}';
    }

    /**
     * 检验 signature
     *
     * @return boolean
     */
    public boolean checkSignature(WxConfig wxConfig) {
        //排序
        String str= SortUtils.dict(timestamp,nonce,wxConfig.getToken());
        //sha1加密
        String sha1Str = EncryptUtils.Sha1(str);
        //判断两者是否相等
        return signature.equals(sha1Str);
    }

}
